package com.ukefu.webim.config.web;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
  
@Configuration  
public class WebServerConfiguration  
{  
    private Integer maxthread = 2000;  
	
    private Integer maxconnections = 2000;   
	
    @Bean  
    public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory()  
    {  
        TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();  
        tomcatFactory.setPort(8081);  
        tomcatFactory.addConnectorCustomizers(new UKeFuTomcatConnectorCustomizer(maxthread, maxconnections));  
        return tomcatFactory;  
    }  
}  
class UKeFuTomcatConnectorCustomizer implements TomcatConnectorCustomizer{
	private Integer maxthread ;
	private Integer maxconnection ;
	UKeFuTomcatConnectorCustomizer(Integer maxthread  , Integer maxconnection){
		this.maxthread = maxthread ;
		this.maxconnection = maxconnection ;
	}
    public void customize(Connector connector)  
    {  
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();  
        //设置最大连接数
        protocol.setMaxConnections(maxthread!=null ? maxthread : 2000);  
        //设置最大线程数  
        protocol.setMaxThreads(maxconnection!=null ? maxconnection: 2000);  
        protocol.setConnectionTimeout(30000);  
    }  
}  